package com.xiaoyy.core.config.set;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * 缓存配置.
 * @author chenzh
 * @version v.0.1
 */
@Configuration
@EnableCaching
public class EhcacheConfig {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    private final static String PATH="config/ehcache.xml";
    /**
     *  ehcache 主要的管理器
     * @param bean
     * @return
     */
    @Bean(name="ehCacheCacheManager")
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
        //System.out.println("CacheConfiguration.ehCacheCacheManager()");
        return new EhCacheCacheManager(bean.getObject());
    }

    /**
     * 据shared与否的设置,
     * Spring分别通过CacheManager.create()
     * 或new CacheManager()方式来创建一个ehcache基地.
     *
     * 也说是说通过这个来设置cache的基地是这里的Spring独用,还是跟别的(如hibernate的Ehcache共享)
     *
     */
    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        //System.out.println("CacheConfiguration.ehCacheManagerFactoryBean()");
        logger.debug("启用ehcache缓存");
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        cacheManagerFactoryBean.setConfigLocation (new ClassPathResource(PATH));
        cacheManagerFactoryBean.setShared(true);
        return cacheManagerFactoryBean;
    }

}  